<?php

class SmsController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $session = new Zend_Session_Namespace('Zend_Auth');
        $this->sess = $session->data;

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        
        $this->_helper->layout->setLayout('sms');        

        if(!isset($this->sess)){


            $param = array(
            'action' => 'index',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );

            
            $this->_redirect($this->view->url($param, 'default', true));
        }

        $this->sms = New Application_Model_Sms();
        
        

    }

    public function indexAction()
    {
        //
    }

    

    public function sendsmsAction()
    {


        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            $memberId = $this->sess->member_id;
            $accNum = $this->sess->member_code;

            //Member Detail
            //$tujuan  = str_replace(";",",",$formData['tujuan']);
            $pesan = $formData['pesan'];
            $stts = "0";

            //$arrTlp = explode(",", $tujuan);

            $memberModel = new Application_Model_Member();
            $downline = $memberModel -> getMemberDownline($memberId);

            $cnt = 0;
            for ($x=0; $x<count($downline); $x++){

                $tlp = trim($downline[$x]['mobile_no']);

                if ($tlp != ""){
                    // Insert Outbox
                    $this->sms->sendSMS($accNum, $tlp, $pesan, $stts);
                    $cnt++;
                }
            }

            $this->view->code = '0';
            $this->view->msg = 'Kirim SMS Sukses ke '.$cnt.' downline';

            /*
            $this->view->code = $code;

            if($code == 0){
                $param = array(
                'action' => 'sendsms',
                'controller' => 'admin',
                'code' => $code,
                'msg' => $msg
                );

                $this->_redirect($this->view->url($param, 'default', true));
            }else{
                $this->view->errorMsg = $msg;
                $this->view->data = $this->param;
            }
             * 
             */


        }

    }


    
}

